package com.tulingxueyuan.mall.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.ums.mapper.UmsMemberReceiveAddressMapper;
import com.tulingxueyuan.mall.modules.ums.model.UmsMemberReceiveAddress;
import com.tulingxueyuan.mall.modules.ums.service.UmsMemberReceiveAddressService;
import com.tulingxueyuan.mall.modules.ums.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 会员收货地址表 服务实现类
 * </p>
 *
 * @author XuShu
 * @since 2024-05-19
 */
@Service
public class UmsMemberReceiveAddressServiceImpl extends ServiceImpl<UmsMemberReceiveAddressMapper, UmsMemberReceiveAddress> implements UmsMemberReceiveAddressService {

    @Autowired
    UmsMemberService memberService;
    /**
     * 收获地址的新增
     *  (method = "post"), (url = "/member/address/add");
     */
    @Override
    public boolean add(UmsMemberReceiveAddress address) {
        address.setMemberId(memberService.getCurrentMember().getId());
        address.setDefaultStatus(0);
        return this.save(address);
    }

    /**
     * 收获地址的修改
     *  (method = "post"), (url = `/member/address/update/${checkedItem.id}`);
     * @param address
     * @return
     */
    @Override
    public boolean edit(UmsMemberReceiveAddress address,Long id) {
        address.setId(id);
        return this.updateById(address);
    }

    /**
     * 删除收货地址
     * (method = "post"), (url = `/member/address/delete/${checkedItem.id}`);
     * @param id
     * @return
     */
    @Override
    public boolean delete( Long id) {
        return this.removeById(id);
    }

    /**
     * 获取收货地址列表
     * this.axios.get("/member/address/list").then((res) => {
     *         this.list = res;
     *       });
     */
    @Override
    public List<UmsMemberReceiveAddress> listByMemberId() {
        QueryWrapper<UmsMemberReceiveAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsMemberReceiveAddress::getMemberId, memberService.getCurrentMember().getId());

        return this.list(queryWrapper);
    }

}
